package newx.com.config
{
	import flash.events.Event;
	
	import newx.BaseObject;
	import newx.com.loader.TxtLoader;
	
	/**
	 * 配置文件管理器：
	 * 
	 * 2009.9.22	：
	 * 1、修改了事件派发方式，现在在目标对象上派发事件时，同时记录了产生时间的类的引用
	 * 
	 * @author riki
	 * 
	 */	
	public class TextIni extends BaseObject
	{
		private var _strData:String = null;
		
		/**
		 * 根据区段名和键名获得信息 
		 * @param section
		 * @param key
		 * @param index
		 * @return 
		 * 
		 */		
		public function getContent(section:String, key:String, index:int = 1):String
		{
			if (this._strData == null)
			{
				return null;
			}
			
			var indexPos:int = this._strData.indexOf("[" + section + "]");
			
			if (indexPos == -1)
			{
				return null;
			}
			var passPos:int = this._strData.indexOf("[", indexPos + section.length + 2);
			var i:int = 0;
			var keyPos:int = indexPos;
			for(i = 0; i < index; i++)
			{
				if (keyPos == -1)
				{
					return null;
				}
				if (i == 0)
				{
			        keyPos = this._strData.indexOf(key, keyPos);
			    }
			    else
			    {
			    	keyPos = this._strData.indexOf(key, keyPos + key.length);
			    }
			}
			
			if (passPos == -1)
			{
				if (keyPos == -1)
				{
					return null;
				}
			}
			else
			{
				if (keyPos == -1 || keyPos > passPos)
				{
					return null;
				}
			}
			
			var endPos:int = this._strData.indexOf("\r\n", keyPos);
			
			if (endPos == -1)
			{
				endPos = this._strData.length;
			}
			
			var returnStr:String = this._strData.substring(keyPos + key.length, endPos);
			
			var equalPos:int = returnStr.indexOf("=");
			
			returnStr = returnStr.substring(equalPos + 1, returnStr.length);
			
			if (returnStr.charAt(0) == " ")
			{
				returnStr = returnStr.substring(1, returnStr.length);
			}
			return returnStr;
		}
		
		/**
		 * 根据区段名和键名获得信息 (取带*号的键值对)
		 * @param str 
		 * @param section
		 * @param key
		 * @param index
		 * @return 
		 * 
		 */		
		public function getStrContent(str:String,section:String, key:String, index:int = 1):String
		{
			if (str == null)
			{
				return null;
			}
			
			var indexPos:int = str.indexOf("[" + section + "]");
			
			if (indexPos == -1)
			{
				return null;
			}
			var passPos:int = str.indexOf("[", indexPos + section.length + 2);
			var i:int = 0;
			var keyPos:int = indexPos;
			for(i = 0; i < index; i++)
			{
				if (keyPos == -1)
				{
					return null;
				}
				if (i == 0)
				{
					keyPos = str.indexOf(key, keyPos);
				}
				else
				{
					keyPos = str.indexOf(key, keyPos + key.length);
				}
			}
			
			if (passPos == -1)
			{
				if (keyPos == -1)
				{
					return null;
				}
			}
			else
			{
				if (keyPos == -1 || keyPos > passPos)
				{
					return null;
				}
			}
			
			var endPos:int = str.indexOf("\r\n", keyPos);
			
			if (endPos == -1)
			{
				endPos = str.length;
			}
			
			var returnStr:String = str.substring(keyPos + key.length, endPos);
			
			var equalPos:int = returnStr.indexOf("=");
			
			returnStr = returnStr.substring(equalPos + 1, returnStr.length);
			
			if (returnStr.charAt(0) == " ")
			{
				returnStr = returnStr.substring(1, returnStr.length);
			}
			return returnStr.substring(1,returnStr.length-1);
		}
		
		public function TextIni(url:String = "",loadCompleteCallBack:Function = null)
		{
			super(null);
			if(url == "")
			{
				return;
			}
			
			var txtloader:TxtLoader = new TxtLoader();
			
			this.HandleProperties("loadCompleteCallBack",loadCompleteCallBack);
			
			txtloader.addEventListener(Event.COMPLETE, onComplete,false,0,true);
			txtloader.load(url);
		}
		
		public function setInitStr(str:String):void
		{
			this._strData = str;
		}
		
		private function onComplete(event:Event):void
		{
			var loader					:TxtLoader 	= 	event.target as TxtLoader;
			var	loadCompleteCallBack	:Function	=	GetProperties("loadCompleteCallBack");
			
            this._strData = loader.strdata;
            
            if(loadCompleteCallBack != null)
            {
            	loadCompleteCallBack.call(this);
            }
			
			DeleteProperties("loadCompleteCallBack");
			
//			GC.gc();
		}
	}
}